<?php
class manage extends MY_Controller
{
	public function __construct()
	{
		log_message('debug', '=====开始调用BattleServer->Manage->__construct=====');
		parent::__construct();
		$this->loadLanguageFile('application/controllers/BattleServer/');
		log_message('debug', '=====调用BattleServer->Manage->__construct结束=====');
	}
	
	public function showView()
	{
		$this->load->view('module/BattleServer/manage.htm');
	}
	
	public function getListData()
	{
		log_message('debug', '=====开始调用BattleServer->Manage->getListData=====');
		$this->verifyPrivilege();
		
		try {
			$page = $this->param->getParam('page', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$rows = $this->param->getParam('rows', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			
			$sql = "
				select
					<selectColumns>
				from
					`t_data_battle_server` a
				left join
					`t_data_operator` b
				on
					a.`operatorId` = b.`operatorId`
				left join
					`t_data_server` c
				on
					a.`serverId` = c.`serverId`
				left join
					`t_data_game_version` d
				on
					a.`versionId` = d.`id`
			";
			
			$getTotalSql = str_replace('<selectColumns>', "count(*)", $sql);
			$this->returnData['data']['total'] = $this->gmDatabase->getColumn($getTotalSql);
			$getDataSql = str_replace('<selectColumns>', "a.`battleServerId`, a.`operatorId`, b.`operatorName`, a.`areaNum`, a.`serverId`, c.`serverName`, `portGroup`, a.`directoryName`, a.`status`, d.`id` as `versionId`, d.`version`, concat(b.`operatorName`, '-', a.`areaNum`) as `battleServerName`", $sql);
				
			if($page && $rows) {
				$startRow = ($page - 1) * $rows;
				$getDataSql .= " limit {$startRow}, {$rows}";
			}
				
			$this->returnData['data']['rows'] = $this->gmDatabase->getArray($getDataSql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用BattleServer->Manage->getListData结束=====');
	}
	
	public function add() {
		log_message('debug', '=====开始调用BattleServer->Manage->add=====');
		$this->verifyPrivilege();
		
		try {
			$operatorId = $this->param->getParam('operatorId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406001, 'invalidErrorCode' => 1));
			$areaNum = $this->param->getParam('areaNum', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406002, 'invalidErrorCode' => 406003));
			$serverId = $this->param->getParam('serverId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406004, 'invalidErrorCode' => 1));
			$portGroup = $this->param->getParam('portGroup', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406005, 'invalidErrorCode' => 406006));
			$directoryName = $this->param->getParam('directoryName', array('method' => 'string', 'emptyErrorCode' => 406007, 'invalidErrorCode' => 406008));
			$versionId = $this->param->getParam('versionId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406009, 'invalidErrorCode' => 1));
			
			$this->vaildPortGroup($serverId, $portGroup);
			
			$sql = "
				insert into
					`t_data_battle_server`
				set
					`operatorId` = '{$operatorId}',
					`areaNum` = '{$areaNum}',
					`serverId` = '{$serverId}',
					`portGroup` = '{$portGroup}',
					`directoryName` = '{$directoryName}',
					`versionId` = '{$versionId}',
					`status` = 1
			";
			$this->gmDatabase->query($sql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$errorCode = $e->getCode();
		
			if($errorCode === 1062) {
				$this->setErrorCode(406013);
			}
			else {
				$this->setErrorCode(1);
			}
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用BattleServer->Manage->add结束=====');
	}
	
	public function modify() {
		log_message('debug', '=====开始调用BattleServer->Manage->modify=====');
		$this->verifyPrivilege();
		
		try {
			$battleServerId = $this->param->getParam('battleServerId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			$operatorId = $this->param->getParam('operatorId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406001, 'invalidErrorCode' => 1));
			$areaNum = $this->param->getParam('areaNum', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406002, 'invalidErrorCode' => 406003));
			$serverId = $this->param->getParam('serverId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406004, 'invalidErrorCode' => 1));
			$portGroup = $this->param->getParam('portGroup', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406005, 'invalidErrorCode' => 406006));
			$directoryName = $this->param->getParam('directoryName', array('method' => 'string', 'emptyErrorCode' => 406007, 'invalidErrorCode' => 406008));
			$versionId = $this->param->getParam('versionId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 406009, 'invalidErrorCode' => 1));
			$status = $this->param->getParam('status', array('method' => 'int', 'min' => 1, 'max' => 4, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			
			$this->vaildPortGroup($serverId, $portGroup, $battleServerId);
			
			$sql = "
				update
					`t_data_battle_server`
				set
					`operatorId` = '{$operatorId}',
					`areaNum` = '{$areaNum}',
					`serverId` = '{$serverId}',
					`portGroup` = '{$portGroup}',
					`directoryName` = '{$directoryName}',
					`versionId` = '{$versionId}',
					`status` = '{$status}'
				where
					`battleServerId` = '{$battleServerId}'
			";
			$this->gmDatabase->query($sql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$errorCode = $e->getCode();
		
			if($errorCode === 1062) {
				$this->setErrorCode(406013);
			}
			else {
				$this->setErrorCode(1);
			}
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用BattleServer->Manage->modify结束=====');
	}
	
	public function install() {
		log_message('debug', '=====开始调用BattleServer->Manage->install=====');
		$this->verifyPrivilege();
		
		try {
			$battleServerId = $this->param->getParam('battleServerId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			
			$sql = "
				select
					`status`
				from
					`t_data_battle_server`
				where
					`battleServerId` = '{$battleServerId}'
			";
				
			$status = $this->gmDatabase->getColumn($sql);
				
			if($status === false) {
				throw new Exception('', 1);
			}
				
			if($status != 1) {
				throw new Exception('', 406014);
			}
			
			$sql = "
				select
					count(*)
				from
					`t_data_battle_server_install_queue`
				where
					`battleServerId` = '{$battleServerId}'
					and
					`status` <= 2
			";
				
			if($this->gmDatabase->getColumn($sql) > 0) {
				throw new Exception('', 406015);
			}
				
			$sql = "
				insert into
					`t_data_battle_server_install_queue`
				set
					`battleServerId` = '{$battleServerId}',
					`operateUserId` = '{$this->currUserId}',
					`operateDatetime` = now(),
					`status` = 1
			";
			$this->gmDatabase->query($sql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用BattleServer->Manage->install结束=====');
	}
	
	private function vaildPortGroup($serverId, $portGroup, $battleServerId = 0) {
		$sql = "
			select
				`maxGameAreaNum`
			from
				`t_data_server`
			where
				`serverId` = '{$serverId}'
		";
		$maxAreaNum = $this->gmDatabase->getColumn($sql);
	
		if($maxAreaNum === false) {
			throw new Exception('', 1);
		}
	
		if($portGroup > $maxAreaNum) {
			throw new Exception('', 406010);
		}
		
		$sql = "
			select
				count(*)
			from
				`t_data_server_portgroup`
			where
				`serverId` = '{$serverId}'
				and
				`portGroup` = '{$portGroup}'
				and
				`status` = 2
		";
		
		if($this->gmDatabase->getColumn($sql) === '1') {
			throw new Exception('', 406011);
		}
		
		$sql = "
			select
				count(*)
			from
				`t_data_battle_server`
			where
				`serverId` = '{$serverId}'
				and
				`portGroup` = '{$portGroup}'
				and
				`battleServerId` <> '{$battleServerId}'
			
		";
		
		if($this->gmDatabase->getColumn($sql) === '1') {
			throw new Exception('', 406012);
		}
	}
}